/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.raitcon.hibernate.db;

import com.raitcon.hibernate.bean.DetalleLibreta;
import com.raitcon.hibernate.bean.DetalleNota;
import com.raitcon.hibernate.bean.HibernateSessionFactory;
import com.raitcon.hibernate.bean.Libreta;
import com.raitcon.hibernate.bean.Nota;
import com.raitcon.hibernate.factory.HibernateUtil;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Mike
 */
public class DetalleLibretaDB {
    
    protected static Logger log = Logger.getLogger(DetalleLibretaDB.class);
    private Session session = null;
    
    
    public DetalleLibretaDB(){
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }
    
    
    
    
    public String insert(DetalleLibreta detalleLibreta ) {
        String result = "";
         System.out.println("metodo insertOrUpdate  detallaLibreta");
         session = HibernateSessionFactory.getSession();
         System.out.println("session: " + session);
         System.out.println("Try");
        
         try {
            System.out.println("metodo insert");
            session = HibernateSessionFactory.getSession();
            System.out.println("session: " + session);
            System.out.println("Try");
            //Transaction ts = session.beginTransaction();
            System.out.println("Transaction");
            session.save(detalleLibreta);
            System.out.println("Save");
            //ts.commit();
            System.out.println("Se inserto correctamente");
        } catch (HibernateException ex1) {
            System.out.println("HibernateException : " + ex1);
        } catch (Exception ex2) {
            System.out.println("Exception : " + ex2);
        } finally {
           // session.close();
        }
         return result;
    }
    
    
     public String delete(DetalleLibreta detalleLibreta ) {
        String result = "";
         session = HibernateSessionFactory.getSession();
         System.out.println("session: " + session);
         System.out.println("Try");
        
         try {
            System.out.println("metodo insert");
            session = HibernateSessionFactory.getSession();
            System.out.println("session: " + session);
            System.out.println("Try");
            Transaction ts = session.beginTransaction();
            System.out.println("Transaction");
            session.delete(detalleLibreta);
            System.out.println("delete");
            ts.commit();
            result="Se eliminó correctamente id"+ detalleLibreta.getIdDetalleLibreta();
        } catch (HibernateException ex1) {
            System.out.println("HibernateException : " + ex1);
        } catch (Exception ex2) {
            System.out.println("Exception : " + ex2);
        } finally {
           // session.close();
        }
         return result;
    }
    
    
    public Libreta findLibreta( Integer idGrado, Integer idAnio){
        
         log.info("Iniciando consulta de Libreta...");
         Libreta nota=null;
        try {
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Libreta as libreta  "
                    + "WHERE id_grado="+idGrado+ " AND id_anio="+ idAnio);

            nota=(Libreta) q.uniqueResult();
             log.info("Consulta de libreta exitosa.");
             return nota;
        } catch (Exception e) {
            log.error("Error:" + e.getMessage());
            e.printStackTrace();
        } finally {
            session.close();
        }
        return nota;
    }
    
}
